package com.campus.common.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.campus.common.result.Result;
import com.campus.common.student.job.entity.Student;
import com.campus.common.student.job.entity.dto.AddStudentDTO;
import com.campus.common.student.mapper.StudentMapper;
import com.campus.common.student.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.common.user.entity.dto.InsertUserDTO;
import com.campus.common.user.entity.pojo.User;
import com.campus.common.user.mapper.AuthMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 学生信息表 服务实现类
 * </p>
 *
 * @author mwb
 * @since 2024-11-17
 */
@Service
@RequiredArgsConstructor
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    private final AuthMapper authMapper;


    @Override
    public Result<Object> addStudent(AddStudentDTO addStudentDTO) {
        Student student = new Student();
        BeanUtils.copyProperties(addStudentDTO,student);
        int i=baseMapper.insert(student);
        if(i>0){
            return Result.success();
        }
        return Result.fail("插入失败");
    }

    @Override
    public Result editStudent(Student student) {
        int i = baseMapper.updateById(student);
        if(i>0){
            return Result.success();
        }
        return Result.fail("修改失败");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result insertStudent(InsertUserDTO insertUserDTO) {
        User user = new User();
        user.setWorkId(insertUserDTO.getWorkId());
        user.setSex(insertUserDTO.getSex());
        user.setUsername(insertUserDTO.getWorkId());
        user.setPassword(insertUserDTO.getWorkId());
        user.setIdentity(0);
        user.setName(insertUserDTO.getName());
        user.setRegisterDate(LocalDateTime.now());

        int insert = authMapper.insert(user);
        if(insert>0){
            User user1 = authMapper.selectOne(new LambdaQueryWrapper<User>()
                    .eq(User::getWorkId, insertUserDTO.getWorkId()));

            Student student=new Student();
            student.setCollege(insertUserDTO.getCollege());
            student.setMajor(insertUserDTO.getMajor());
            LocalDate date=LocalDate.now();

            DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy");
            String year=date.format(formatter);
            student.setEnrollmentYear(year);
            student.setUserId(user1.getId());
            student.setStuClass(insertUserDTO.getStuClass());
            int insert1 = baseMapper.insert(student);

            if(insert1>0){
                return Result.success(insertUserDTO);
            }else{
                return Result.fail("插入学生信息失败");
            }
        }else{
            return Result.fail("插入用户失败");
        }

    }
}
